package seismosurfer.gui.menu;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Iterator;

import javax.swing.ButtonGroup;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import com.bbn.openmap.MapHandler;
import com.bbn.openmap.gui.AbstractOpenMapMenu;
import com.bbn.openmap.gui.OpenMapFrame;
import com.bbn.openmap.util.Debug;

/**
 * The LookAndFeelMenu creates menu items to 
 * change the look and feel of the GUI.
 *
 */
public class LookAndFeelMenu extends AbstractOpenMapMenu implements
        ActionListener {
    
    private static final long serialVersionUID = -401786489802379043L;

    // Some constants to set the Look and Feel
    final String JavaLF = UIManager.getCrossPlatformLookAndFeelClassName();

    final String SystemLF = UIManager.getSystemLookAndFeelClassName();

    // final String GTKLF = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel"; //only
    // with JDK 1.4.2
    final String WindowsLF = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";

    final String MotifLF = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";

    private String defaultText = "Look&Feel";

    private int defaultMnemonic = 'F';

    protected final static String javaCmd = "JavaLookAndFeel";

    protected final static String windowsCmd = "WindowsLookAndFeel";

    protected final static String motifCmd = "MotifLookAndFeel";

    protected final static String systemCmd = "SystemLookAndFeel";

    protected OpenMapFrame frame = null;

    public LookAndFeelMenu() {

        super();
        setText(defaultText);
        setMnemonic(defaultMnemonic);

        // Create the radio buttons menu items.
        JRadioButtonMenuItem JavaLookandFeelMenuItem = new JRadioButtonMenuItem(
                "Java");
        JavaLookandFeelMenuItem.setSelected(true);
        JavaLookandFeelMenuItem.setMnemonic(KeyEvent.VK_J);
        JavaLookandFeelMenuItem.setActionCommand(javaCmd);
        add(JavaLookandFeelMenuItem);

        JRadioButtonMenuItem WindowsLookandFeelMenuItem = new JRadioButtonMenuItem(
                "Windows");
        WindowsLookandFeelMenuItem.setMnemonic(KeyEvent.VK_W);
        WindowsLookandFeelMenuItem.setActionCommand(windowsCmd);
        add(WindowsLookandFeelMenuItem);

        JRadioButtonMenuItem MotifLookandFeelMenuItem = new JRadioButtonMenuItem(
                "Motif");
        MotifLookandFeelMenuItem.setMnemonic(KeyEvent.VK_M);
        MotifLookandFeelMenuItem.setActionCommand(motifCmd);
        add(MotifLookandFeelMenuItem);

        JRadioButtonMenuItem SystemLookandFeelMenuItem = new JRadioButtonMenuItem(
                "System");
        SystemLookandFeelMenuItem.setMnemonic(KeyEvent.VK_S);
        SystemLookandFeelMenuItem.setActionCommand(systemCmd);
        add(SystemLookandFeelMenuItem);

        // Group the radio buttons menu items.
        ButtonGroup group = new ButtonGroup();
        group.add(JavaLookandFeelMenuItem);
        group.add(WindowsLookandFeelMenuItem);
        group.add(MotifLookandFeelMenuItem);
        group.add(SystemLookandFeelMenuItem);

        // Register a listener for the radio buttons menu items.
        JavaLookandFeelMenuItem.addActionListener(this);
        WindowsLookandFeelMenuItem.addActionListener(this);
        MotifLookandFeelMenuItem.addActionListener(this);
        SystemLookandFeelMenuItem.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();        

        if (frame == null) {

            MapHandler mapHandler = getMapHandler();
            if (mapHandler == null) {
                System.out.println("NULL MapHandler");
                return;
            }
            
            Iterator it = mapHandler.iterator();
            while (it.hasNext()) {
                Object someObj = it.next();
                if (someObj instanceof OpenMapFrame) {
                    frame = (OpenMapFrame) someObj;
                    break;
                }
            }

            if (frame == null) {
                Debug.error("Couldn't find OpenMapFrame");
                return;
            }
        }        

        if (command.equals(javaCmd)) {
            try {
                UIManager.setLookAndFeel(JavaLF);
                SwingUtilities.updateComponentTreeUI(frame);
                frame.pack();
            } catch (Exception ex) {
                ex.printStackTrace();
                System.out.println(ex.getMessage());
            }
        }

        else if (command.equals(windowsCmd)) {
            try {
                UIManager.setLookAndFeel(WindowsLF);
                SwingUtilities.updateComponentTreeUI(frame);
                frame.pack();
            } catch (Exception ex) {
                ex.printStackTrace();
                System.out.println(ex.getMessage());
            }
        }

        else if (command.equals(motifCmd)) {
            try {
                UIManager.setLookAndFeel(MotifLF);
                SwingUtilities.updateComponentTreeUI(frame);
                frame.pack();
            } catch (Exception ex) {
                ex.printStackTrace();
                System.out.println(ex.getMessage());
            }
        }

        else if (command.equals(systemCmd)) {
            try {
                UIManager.setLookAndFeel(SystemLF);
                SwingUtilities.updateComponentTreeUI(frame);
                frame.pack();
            } catch (Exception ex) {
                ex.printStackTrace();
                System.out.println(ex.getMessage());
            }
        }        

    }

    public void findAndInit(Object someObj) {
        super.findAndInit(someObj);
        if (someObj instanceof OpenMapFrame) {
            frame = (OpenMapFrame) someObj;
        }
    }

    public void findAndUndo(Object someObj) {
        super.findAndUndo(someObj);
        if (someObj instanceof OpenMapFrame) {
            frame = null;
        }

    }

}
